<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>防抖</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <style>
        .container{
            height: 300px;
            background-color: orange;
        }
        p{
            width: 50px;
            font-size: 100px;
            text-align: center;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container" v-on:mousemove="move">
            <p>{{time}}</p>
        </div>
    </div>
    
    <script>
        
        /* 
        *防抖: 每次进行事件调用，重新计时
        *函数被触发多次，但是只执行一次，执行最新的触发。也就是在时间n内，碰到新的触发，就清除之前的，重新计时
        * 
        */
       var app = new Vue({
           el:"#app",
           data:{
                time:0,
                clearTimer:null,
           },
           methods: {
                move(){
                    if(this.clearTimer){
                        clearTimeout(this.clearTimer)
                        this.clearTimer=null
                    };
                    this.clearTimer = setTimeout(()=>this.time++,1000);

                    
                }
           },
       });
        

    </script>
</body>

</html>